//调用站点维护判断接口

function sideJude(){
    $.ajax({
        'type':'post',
        'url':WEBURL+'/maintain/siteInfo',
        'dataType':'json',
        'data': {
            'userCode': sessionStorage.userCode
        },
        success:function(r){

            if(r.code==0000){
               // $("#siteName").html('');
                if(r.data==''||r.data==undefined||r.data==null||r.data=='undefined'){

                    $.Huimodalalert('未查询到该站长的相关站点信息！',2000);
                    $("#siteName").html('未查询到该站长的相关站点信息');
                    $(".searchsiteBtn").attr("disabled",'disabled');
                    $(".searchsiteBtn,.zdwhDcBtn").css({"background":'#c7c7c7',"border":'1px solid #c7c7c7'});
                    //$(".zdwhDcBtn").removeClass('zdwhDcBtn');
                }
                else{
                    $(".zdwhDcBtn").addClass('zdwhDcBtnshow');
                    sessionStorage.zdwhSideId = r.data.orgCode;
                    sessionStorage.zdwhSideName = r.data.orgName;
                   $("#siteName").html(r.data.orgName+'站点运力详情');
                }

            }
        }

    })

}
$(function(){

    //初始化 返回站点id
    sideJude();

    var orginDate = new Array();
    $("#zdwh-examine").on("click",function(){
        $("#deliveryList tr").find(".eidt").attr("contenteditable","false");
        $("#deliveryList tr").find(".eidt").css("background","none");


        if($("#deliveryList").find("input:checked").length==0){

            $.Huimodalalert('至少选择一条记录！',2000);

            return false;
        }

        for(var i =0;i<$("#deliveryList").find("tr").length;i++){

            if($("#deliveryList tr").eq(i).children("td").find("input").is(":checked")){
                $("#deliveryList tr").eq(i).find(".eidt").children("input").show();
                $("#zdwh-save").show();
                $("#zdwh-reset").show();
                $(this).hide();
            }

        }
    });

//取消
    $("#zdwh-reset").on("click",function(){
        $("#zdwh-examine").show();
        $("#taby-body-list tr").find(".eidt").children("input").hide();
        $("#zdwh-save").hide();
        $(this).hide();

    });




})

//站点维护查询列表

function showInfoList(startTime,endTime,id){
    var tablehead='<table class="table table-border table-bordered table-hover table-sort"><thead><tr class="text-c table-head-bg"><th>站点</th><th class="sorting_asc">总单量</th><th class="sorting_asc">人效单量</th> <th class="sorting_asc">出勤人数</th><th class="sorting_asc">站点人效</th><th class="sorting_asc">普通件</th><th class="sorting_asc">大学件（核算）</th><th class="sorting_asc">大学件（原始）</th><th class="sorting_asc">临时工件</th><th class="sorting_asc">临时工</th><th class="sorting_asc">批量件</th><th class="sorting_asc">日期</th><th>操作</th></tr></thead>'
    $.ajax({
        type:"get",
        url:WEBURL+"/maintain/list",
        dataType: 'json',
        data: {
            'beginTime': startTime,
            'endTime': endTime,
            'siteId':id
        },
        success:function(r){
            if(r.code==0000){
                $("#taby-body-list").html(' ');
                $("#datalst").html('');
                if(r.data.length>0){

                    var zdwhList ='';
                    $.each(r.data, function(index,item) {
                        //console.log(r.data[index].orderTotalNum);
                        zdwhList = zdwhList+'<tr class="text-c"><td>'
                            +item.siteName+'</td><td class="eidt">'
                            +item.orderTotalNum+'</td><td class="eidt">'
                            +item.orderEfficiencyNum+'</td><td class="eidt">'
                            +item.workerNum+'</td><td>'
                            +item.siteEfficiency+'</td><td class="eidt">'
                            +item.orderCommonNum+'</td><td class="eidt">'
                            +item.dxOrderNumAdjust+'</td><td class="eidt">'
                            +item.dxOrderNumOriginal+'</td><td class="eidt">'
                            +item.tempOrderNum+'</td><td class="eidt">'
                            +item.tempDeliveryNum+'</td><td class="eidt">'
                            +item.batchOrderNum+'</td><td>'+item.siteDate+'</td><td><span onClick=\'system_category_add("'+item.siteName+'","zdwh-repair.html?id='+item.siteDetailId+'",1100,600)\'  class="btn btn-secondary radius">查看</span></td></tr>';
                    });


                }
                else{

                    zdwhList = '';
                }
                //console.log(zdwhList);
                zdwhList = '<tbody id="taby-body-list">'+zdwhList+'</tbody></table>';
                var table_show = tablehead+zdwhList;

                $(table_show).appendTo($("#datalst"));
                if($("#datalst").html()!=''){
                    tableSort();
                }
            }
        },
        error:function(e){e.msg}
    });


}

//排序
function tableSort(){

    $('.table-sort').dataTable({
      // "aaSorting": [[1, "asc"]],//默认第几个排序
        "bStateSave": false,//状态保存
        "bPaginate":false,
        "bProcessing" : true,//加载进度提示(默认：false)
        "aoColumnDefs": [
            {"orderable":false,"aTargets":[0,0]},// 不参与排序的列
            {"orderable":false,"aTargets":[0,1]},// 不参与排序的列
            {"orderable":false,"aTargets":[0,12]},// 不参与排序的列
            {"orderable":false,"aTargets":[0,11]}// 不参与排序的列
        ]
    });
}

$(function(){
  //  showInfoList("2017-08-01","2017-08-31","1002");



    //查询条件
    $(".searchsiteBtn").on("click",function(){
        var starttime =$("#starttime").val();
        var endtime = $("#endtime").val();

        if(starttime>endtime){
            $.Huimodalalert('结束时间不能小于开始时间,请重新选择！',2000);
            return;
        }

        var siteId = sessionStorage.zdwhSideId;//站点ID
        if(starttime==''||starttime==null||starttime=="null"||starttime==undefined||starttime=='undefined'){
            $.Huimodalalert('请选择时间！',2000);
            return;
        }

        if(starttime!=''||endtime!=''||siteId!=''){
            showInfoList(starttime,endtime,siteId);

        }

    })

    //重置
    $(".resetBtn").on("click",function(){

        $("#starttime").val('');
        $("#endtime").val('');
    })

	//点击查看或者数据列表

    //导出数据
    $(".zdwhDcBtn").on("click",function(){
        if($(this).hasClass('zdwhDcBtnshow')==false){
            return;
        }


        if($("#starttime").val()==''){
            $.Huimodalalert('请选择时间！',2000);
            return false;
        };
        if($("#endtime").val()==''){
            $.Huimodalalert('请选择时间！',2000);
            return false;
        };
        var timeLoad =   window.setTimeout(function(){
            $.Huimodalalert('数据下载中,请稍后。。。。',2000);
        },200);
        // $("#zdwh_dc input[name=beginTime]").val( $("#starttime").val());
        // $("#zdwh_dc input[name=endTime]").val( $("#endtime").val());
        //
        // $("#zdwh_dc input[name=siteId]").val(sessionStorage.zdwhSideId);
        // $("#zdwh_dc").attr("action",WEBURL + "/maintain/export");
        // $("#zdwh_dc").submit();
        var oFrm = document.getElementById('iframe');
        oFrm.onload = oFrm.onreadystatechange = function() {
            if (this.readyState && this.readyState != 'complete') return;
            else {
                window.clearTimeout(timeLoad);
                alert('未查询到数据！');
            }
        }
        var url =WEBURL +'/maintain/export?beginTime='+$("#starttime").val()+'&endTime='+$("#endtime").val()+'&siteId='+sessionStorage.zdwhSideId;
        $("#iframe").attr("src",url);


    })

})
